package com.mars.algorithm;

/**
 * @Author King
 * @create 2020/6/22 14:04
 */
public class Pattern {

    public static void main(String[] args) {
        System.out.println(check("a", ""));
        System.out.println(check("a", "a"));
        System.out.println(check("a", "got"));
        System.out.println(check("aa", "gotgot"));
        System.out.println(check("aaa", "gotgotgot"));
//
        System.out.println(check("b", ""));
        System.out.println(check("b", "got"));
        System.out.println(check("bb", "gotgot"));
        System.out.println(check("bbb", "gotgtgot"));


        System.out.println("######################################");
        System.out.println(check("abbab", "gotjkjkgotjk"));
        System.out.println(check("abbab", "hotjkjkgotjk"));
        System.out.println(check("abbaba", "hotjkjkhotjkhot"));
        System.out.println(check("abbabaa", "hotjkjkhotjkhothot"));
        System.out.println(check("abbababbb", "hotjkjkhotjkhotjkjkjk"));
        System.out.println(check("abbaba", "hotjkjkhotjkhot"));
        System.out.println(check("bba", "jkhothot"));
        System.out.println(check("bab", "jkhotjk"));
        System.out.println(check("abbababa", "hotjkjkhotjkhotjkgoy"));
        System.out.println(check("babbaba", "jkhotjkjkhotjkhot"));
        System.out.println(check("bbabbaba", "jkjkhotjkjkhotjkhot"));
        System.out.println(check("abbabaaaa", "hotjkjkhotjkhothothothot"));

    }

    private static boolean check(String pattern, String value) {
        if ((null == pattern || "".equals(pattern)) && (null != value && !"".equals(value))) {
            //判断极端的情况
            return false;
        }
        if (pattern.equals(value)) {
            //匹配串和字符串相等的时候
            return true;
        }
        //考虑字符只有a或b模式串的时候
        int aCount = 0;
        int bCount = 0;
        char[] pArray = pattern.toCharArray();
        char[] vArray = value.toCharArray();
        for (int i = 0; i < pArray.length; i++) {
            if (97 == pArray[i]) {
                aCount++;
            } else {
                bCount++;
            }
        }
        if (aCount == 0 || bCount == 0) {
            if ("".equals(value)) {
                return true;
            }
            if (aCount == 0) {
                return onlyOne(bCount, value);
            } else if (bCount == 0) {
                return onlyOne(aCount, value);
            }
        }
        if (onlyOne(aCount, value) && value.length() > 0) {
            return true;
        }

        if (onlyOne(bCount, value) && value.length() > 0) {
            return true;
        }

        //判断ab串都存在的情况
        int aLength = 1;
        int bLength = 1;
        while (aLength * aCount < vArray.length) {
            if ((vArray.length - aLength * aCount) % bCount != 0) {
                aLength++;
                continue;
            }
            bLength = (vArray.length - aLength * aCount) / bCount;
            if (bothAAndB(pArray, vArray, aLength, bLength, pattern, value)) {
                return true;
            }
            aLength++;
        }
        return false;
    }


    private static boolean onlyOne(int count, String value) {
        //只有a或b的情况
        if (value.length() % count != 0) {
            return false;
        }
        int n = value.length() / count;
        String sb = value.substring(0, n);
        for (int k = 0; k < value.length(); k += n) {
            if (!sb.equals(value.substring(k, k + n))) {
                return false;
            }
        }
        return true;
    }


    private static boolean bothAAndB(char[] pattern, char[] value, int aLength, int bLength, String p, String v) {
        //ab都存在的情况
        int aIndex = -1, bIndex = -1, i = 0, idx = 0;
        while (aIndex == -1 || bIndex == -1) {
            if (pattern[i] == 97) {
                if (aIndex == -1) {
                    aIndex = idx;
                }
                idx += aLength;
            } else {
                if (bIndex == -1) {
                    bIndex = idx;
                }
                idx += bLength;
            }
            i++;
        }
        String aStr = v.substring(aIndex, aIndex + aLength);
        String bStr = v.substring(bIndex, bIndex + bLength);
        //接下来遍历模式匹配串，因为这个时候a,b代表的串已经确定了
        int j = 0;
        int change = 0;
        for (int m = 0; m < pattern.length; m++, j += change) {
            if (97 == pattern[m]) {
                change = aLength;
                if (!aStr.equals(v.substring(j, j + change))) {
                    return false;
                }
            } else {
                change = bLength;
                if (!bStr.equals(v.substring(j, j + change))) {
                    return false;
                }
            }
        }
        return true;
    }
}
